package com.ruoyi.common.enums.common;

import com.baomidou.mybatisplus.annotation.IEnum;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.ruoyi.common.config.jackson.IEnumDeserializer;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.EnumUtils;
import com.ruoyi.common.utils.collection.Lists;
import lombok.Getter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * 任务状态枚举
 *
 * @author Gua
 * @version 1.0
 * @date 2021-11-04
 */
@Getter
@JsonDeserialize(using = IEnumDeserializer.class)
public enum TaskStatusEnum implements IEnum<Integer> {
    //未开始
    NOT_START(0, "未开始"),
    //执行中
    EXECUTING(1, "执行中"),
    //暂停
    STOP(4, "暂停"),
    //执行失败
    EXECUTED_FAIL(7, "执行失败"),
    //执行完成
    EXECUTED_FINISH(9, "执行完成");

    private static final Map<TaskStatusEnum, Set<TaskStatusEnum>> TRANSITION_MAP;

    static{
        TRANSITION_MAP = new HashMap<>();
        TRANSITION_MAP.put(NOT_START, Stream.of(EXECUTING).collect(Collectors.toSet()));
        TRANSITION_MAP.put(EXECUTING, Stream.of(STOP,EXECUTED_FAIL,EXECUTED_FINISH).collect(Collectors.toSet()));
        TRANSITION_MAP.put(STOP, Stream.of(EXECUTING).collect(Collectors.toSet()));
        TRANSITION_MAP.put(EXECUTED_FAIL, Stream.of(EXECUTING).collect(Collectors.toSet()));
    }

    @JsonValue
    private int value;
    private String description;

    TaskStatusEnum(int value, String description) {
        this.value = value;
        this.description = description;
    }

    @Override
    public Integer getValue() {
        return value;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return description;
    }

    /**
     * 当前枚举是否是活跃状态
     * @param value 当前枚举value值
     * @return
     */
    public static boolean isActive(Integer value){
        if(StringUtils.isNotNull(value)){
            TaskStatusEnum taskStatusEnum = EnumUtils.toEnum(TaskStatusEnum.class, value);
            if(taskStatusEnum==null){
                return false;
            }
            return taskStatusEnum.isActive();
        }
        return false;
    }

    /**
     * 当前枚举是否是活跃状态
     * @return
     */
    public boolean isActive(){
        return getActiveTaskStatusEnumList().contains(this);
    }

    /**
     * 返回代表活跃任务状态 集合
     * @return
     */
    public static Set<TaskStatusEnum> getActiveTaskStatusEnumList(){
        return Stream.of(TaskStatusEnum.EXECUTING,TaskStatusEnum.STOP,TaskStatusEnum.EXECUTED_FAIL).collect(Collectors.toSet());
    }
    /**
     * 判断当前状态是否可以过渡到下一个状态
     * @param target 目标状态
     * @return true可以过渡到目标状态 false 不能够过渡到目标状态
     */
    public  boolean isTransition(TaskStatusEnum target){
        TaskStatusEnum source = this;

        return TRANSITION_MAP.containsKey(source)
                && TRANSITION_MAP.get(source).contains(target);
    }

}
